#include "stdafx.h"
#include "Solution.h"

std::vector<int> Solution::xorQueries(std::vector<int>& arr, std::vector<std::vector<int>>& queries)
{
    // xor[Li, Ri]
    // xor[0, Ri] ^ xor[0, Li - 1]  if (Li >= 1)
    // xor[0, Ri]                         if (Li == 0)

    std::vector<int> dp(arr.size(), arr[0]);

    // [dp[0], dp[i]) has done
    for (ptrdiff_t i = 1; i < static_cast<ptrdiff_t>(arr.size()); ++i) {
        dp[i] = dp[i - 1] ^ arr[i];
    }
    // [dp[0], dp[arr.size()]) has done

    std::vector<int> results{};
    for (const auto &query : queries) {
        int Li = query[0];
        int Ri = query[1];

        if (Li >= 1) {
            results.push_back(dp[Ri] ^ dp[Li - 1]);
        }
        else {
            results.push_back(dp[Ri]);
        }
    }

    return results;
}
